package com.blog.zip.controller.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.blog.zip.bean.Comment;
import com.blog.zip.bean.RespBean;
import com.blog.zip.bean.User;
import com.blog.zip.mapper.ArticleMapper;
import com.blog.zip.mapper.CategoryMapper;

import com.blog.zip.mapper.TagMapper;
import com.blog.zip.mapper.UserMapper;
import com.blog.zip.service.ArticleService;
import com.blog.zip.service.GitalkService;
import com.blog.zip.service.QueryCollectionService;
import com.blog.zip.service.QueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Slf4j
@RestController
public class WebBlogInfo {

    @Autowired
    private
    UserMapper userMapper;

    @Autowired
    private GitalkService gitalkService;

    @Autowired
    private static int cookieCount;

    @Autowired
    private QueryService queryService;

    @Autowired
    private QueryCollectionService queryCollectionService;

    @GetMapping("/blog/info/get")
    public Map<String, Object> getTags(HttpServletResponse response) {
        Map blogIngo = new HashMap();
        Integer articleNum = userMapper.getBlogInfo(1);
        Integer tagNum = userMapper.getBlogInfo(2);
        blogIngo.put("articleNum", articleNum);
        blogIngo.put("tagNum", tagNum);


       // Cookie cookie = new Cookie("name","瓜田李下3");

        //cookie.setMaxAge(7 * 24 * 60 * 60);
        // 发送http请求，获取博客的基本信息 ip 和浏览量
        JSONObject jsonObject = GitalkService.getDataByCityName();
        //log.info("{}",jsonArray);
        // 获取偏好设置
        List list = userMapper.getBlogPreference("preference");
        blogIngo.put("blogInfo", list);
        blogIngo.put("tongji",jsonObject);

        // 调用MongoDB 取出所有的评论数
//         queryCollectionService.getCollectionNames();
        //log.info("{}",obj);
        //response.addCookie(cookie);
        // 种cookie
        List<Comment> comments = queryService.findAllComments();
//        log.info("{}",comments);
//
//        for (Comment comment : comments) {
//            log.info("用户信息：{}", user);
//        }

        blogIngo.put("comments",comments.size());

        Map<String, Object> map = new HashMap();
        map.put("code", 200);
        map.put("data", blogIngo);


        return map;
    }

    @PostMapping("/blog/info/update")
    public RespBean updateBlogInfo(@RequestBody Map<String, Object> m) {
//        Map blogIngo = new HashMap();
//          userMapper.updateBlogInfo(m);


        for (String key : m.keySet()) {
            Map<String,Object> payload =  new HashMap<>();
            payload.put("key",key);
            payload.put("value",m.get(key));
            userMapper.updateBlogInfo(payload);
        }

//        Integer tagNum = userMapper.getBlogInfo(2);
//        blogIngo.put("articleNum", articleNum);
//        blogIngo.put("tagNum", tagNum);
//
//        // 获取偏好设置
//        List list = userMapper.getBlogPreference("preference");
//        blogIngo.put("blogInfo", list);
//        Map<String, Object> map = new HashMap();
//        map.put("code", 200);
//        map.put("data", blogIngo);
        return new RespBean(200, "update success");
    }
}
